#! env python
#-*- coding: utf-8 -*-

import requests

def check(url='http://localhost:11434'):
    ''' check ollama is running. '''
    response = requests.get(url)
    if response.status_code == 200:
        print(response.text)


def list(url='http://localhost:11434'):
    ''' list the models. '''
    response = requests.get(f'{url}/v1/models')
    if response.status_code == 200:
        data = response.json()
        return data['data']


def generate(model, prompt, url='http://localhost:11434'):
    ''' generate data '''
    data = { "model": model, "prompt": prompt }
    print(data)
    response = requests.post(f'{url}/api/generate', data)
    if response.status_code == 200:
        print(response.text)
        return response
    print(response)


def chat(model, messages, url='http://localhost:11434'):
    ''' chat model '''
    data = { "model": model, "message": messages }
    response = requests.post(f'{url}/api/chat', data)
    if response.status_code == 200:
        return response
    else:
        print(f'status code = {response.status_code}')


# check()
# models = list()
# print([m['id'] for m in models])

# res = generate('deepseek-r1:latest', 'who are you?')
chat('deepseek-r1:lateset', [])